package com.feihong.txgw.shopcar.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.feihong.txgw.R;
import com.feihong.txgw.adapter.Holder.BaseViewHolder;
import com.feihong.txgw.model.bean.deliveryAdr.DeliveryAdrEntity;
import com.feihong.txgw.model.greendao.ShopCarGoodsModel;
import com.feihong.txgw.shopcar.viewholder.CreateOrderGoodsHeadViewHolder;
import com.feihong.txgw.shopcar.viewholder.CreateOrderGoodsViewHolder;
import com.feihong.txgw.ui.activity.order.ToCreateOrderActivity;
import com.feihong.txgw.ui.activity.user.MyDeliveryAdrActivity;
import com.feihong.txgw.utils.adress.StringEmpty;
import com.feihong.txgw.utils.kaika.KaikaIntentType;

import java.util.ArrayList;
import java.util.List;

/**
 * @name txgw_app
 * @class name：com.feihong.txgw.shopcar.adapter
 * @class describe:  购物车  创建订单adapter
 * @author wangziheng
 * @time 2017/8/1 10:55
 * @change
 * @chang time
 * @class describe
 */
public class CreateOrderGoodsAdapter extends RecyclerView.Adapter<BaseViewHolder> {

    private static final int TYPE_HEAD = 1;
    private static final int TYPE_LIST = 2;
    private ToCreateOrderActivity mContext;
    private List<ShopCarGoodsModel> mGoodsList;
    private DeliveryAdrEntity mDeliveryAdrEntity;
    public CreateOrderGoodsAdapter(Context context){
        mContext = (ToCreateOrderActivity) context;
        mGoodsList = new ArrayList<>();
    }

    public void setGoodsList(List<ShopCarGoodsModel> goodsList){
        mGoodsList = goodsList;
        notifyDataSetChanged();
    }

    public void setDeliveryAdr(DeliveryAdrEntity deliveryAdr){
        mDeliveryAdrEntity = deliveryAdr;
        notifyDataSetChanged();
    }
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_HEAD) {
            return new CreateOrderGoodsHeadViewHolder(LayoutInflater.from(mContext).inflate(R.layout.shopcar_createorder_recycler_head_layout, parent, false));
        } else {
            return new CreateOrderGoodsViewHolder(LayoutInflater.from(mContext).inflate(R.layout.shopcar_create_order_goods_list_layout, parent, false), mContext);
        }
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        if(getItemViewType(position) == TYPE_LIST){
            CreateOrderGoodsViewHolder mHolder = (CreateOrderGoodsViewHolder) holder;
            mHolder.initData(mGoodsList);
            mHolder.mLeaveMsg.addTextChangedListener(new EditChangedListener(position - 1));
        }else if(getItemViewType(position) == TYPE_HEAD){
            CreateOrderGoodsHeadViewHolder mHolder = (CreateOrderGoodsHeadViewHolder) holder;
            if(mDeliveryAdrEntity != null && !StringEmpty.isEmpty(mDeliveryAdrEntity.getAddress()) && !StringEmpty.isEmpty(mDeliveryAdrEntity.getLocation_info())){
                mHolder.mToAddAdr.setVisibility(View.GONE);
                mHolder.mAdrLayout.setVisibility(View.VISIBLE);
                mHolder.mAdrIcon.setVisibility(View.VISIBLE);
                mHolder.mAdrArr.setVisibility(View.VISIBLE);
                mHolder.mDeliveryAdrTel.setText(mDeliveryAdrEntity.getMobile());
                String adr = (StringEmpty.isEmpty(mDeliveryAdrEntity.getAddress()) ? "" : mDeliveryAdrEntity.getAddress())
                        +(StringEmpty.isEmpty(mDeliveryAdrEntity.getHouse_num()) ? "" : mDeliveryAdrEntity.getHouse_num());
                mHolder.mDeliveryAdrAdr.setText(adr);
                mHolder.mDeliveryAdrName.setText(mDeliveryAdrEntity.getConsignee());
            }else {
                mHolder.mToAddAdr.setVisibility(View.VISIBLE);
                mHolder.mAdrLayout.setVisibility(View.GONE);
                mHolder.mAdrIcon.setVisibility(View.GONE);
                mHolder.mAdrArr.setVisibility(View.GONE);
            }
            mHolder.mAdrView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, MyDeliveryAdrActivity.class);
                    intent.putExtra(KaikaIntentType.ADRESS_INTENT_TYPE_KEY,KaikaIntentType.ADDRESS_INTENT_TYPE_ITEN_CLICK_BACK);
                    mContext.startActivityForResult(intent, MyDeliveryAdrActivity.ADR_ITEMCLICK_REQUEST_CODE);
                    mContext.overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0) {
            return TYPE_HEAD;
        } else {
            return TYPE_LIST;
        }
    }

    class EditChangedListener implements TextWatcher {

        private int position;
        public EditChangedListener(int index){
            position = index;
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            mOnMsgEditChangeListener.setOnMsgChange(position,s.toString());
        }
    }

    private OnMsgEditChangeListener mOnMsgEditChangeListener;
    public void setOnMsgEditChangeListener(OnMsgEditChangeListener onMesEditChangeListener){
            mOnMsgEditChangeListener = onMesEditChangeListener;
    }
    public interface OnMsgEditChangeListener{
        void setOnMsgChange(int position,String msg);
    }
}
